package cn.gok.homework;

import java.io.*;
import java.util.*;

class School implements Serializable {
    private String name;
    private String address;
    private int birthYear;

    public School(String name, String address, int birthYear) {
        this.name = name;
        this.address = address;
        this.birthYear = birthYear;
    }

    @Override
    public String toString() {
        return "School{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", birthYear=" + birthYear +
                '}';
    }
}

class MyStudentCompartor implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.getSid() == o2.getSid() ? (o1.getAge() == o2.getAge() ? 1 : o1.getAge() - o2.getAge()) : o1.getSid() - o2.getSid();
    }
}

class PetCompartor implements Comparator<Pet> {
    @Override
    public int compare(Pet o1, Pet o2) {
        return o1.getName().equals(o2.getName()) ? 0 : o1.getFeetCount() == o2.getFeetCount() ? 1 : o2.getFeetCount() - o1.getFeetCount();
    }
}

class Student {
    private int sid;
    private String name;
    private int age;


    public Student(int sid, String name, int age) {
        this.sid = sid;
        this.name = name;
        this.age = age;
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

class Pet {
    private String name;
    private int feetCount;

    public Pet(String name, int feetCount) {
        this.name = name;
        this.feetCount = feetCount;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getFeetCount() {
        return feetCount;
    }

    public void setFeetCount(int feetCount) {
        this.feetCount = feetCount;
    }

    @Override
    public String toString() {
        return "Pet{" +
                "name='" + name + '\'' +
                ", feetCount=" + feetCount +
                '}';
    }
}

class Family {
    private String owner;
    private TreeSet<Pet> pets;

    public Family(String owner, TreeSet<Pet> pets) {
        this.owner = owner;
        this.pets = pets;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public TreeSet<Pet> getPets() {
        return pets;
    }

    public void setPets(TreeSet<Pet> pets) {
        this.pets = pets;
    }

    @Override
    public String toString() {
        return "Family{" +
                "owner='" + owner + '\'' +
                ", pets=" + pets +
                '}';
    }
}

public class HomeWork{
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //1 创建类 School类：name,address,  birthYear成立的年份：int （2002；2010）
        //    实现序列化接口，void writeToFile();//采用对象流写入将两个school对象写入到 1.dat中，
        //    void readFromFile();//从1.dat中读取数据到对象，并显示出来
        School s1 = new School("a", "ad", 123);
        School s2 = new School("b", "bd", 456);
        writeToFile(s1, s2, "1.dat");
        readFromFile("1.dat");
        //2 创建类 Student：  int sid(学号) String name(姓名) ,int age;
        //    要求使用两种方法实现自定义比较规则： 先按学号排，如果学号学号重复，按年龄排。
        //    并遍历输出结果
        TreeSet<Student> set = new TreeSet<>(new MyStudentCompartor()); //传入自定义比较器
        set.add(new Student(1, "A", 20));
        set.add(new Student(1, "B", 30));
        set.add(new Student(3, "C", 40));
        set.add(new Student(3, "D", 40));
        set.add(new Student(2, "E", 60));
        set.stream().forEach(System.out::println);
        //3  【提高】创建类 Pet: String name(宠物的名字) ,int feetCount（脚的个数）
        //    设计类Family：String owner（主人），此外，每个家庭可以养有多只宠物（使用什么样的集合去存储宠物？？）。
        //    a 创建2个family对象，分别拥有2只和3只宠物
        //    b 写静态方法  ：   public static switchPets(Family f1,Famliy f2):  将两个家庭 脚最多的宠物进行互换。并输出互换后的结果。
        //    特别注意：如果有多只宠物的脚条数一样，任选一只进行调换即可。
        Family family1 = new Family("A", new TreeSet<>(new PetCompartor()));
        family1.getPets().add(new Pet("鸭子", 2));
        family1.getPets().add(new Pet("小鸭子", 2));
        family1.getPets().add(new Pet("三脚兽", 3));
        family1.getPets().add(new Pet("小狗", 4));


        Family family2 = new Family("B", new TreeSet<>(new PetCompartor()));
        family2.getPets().add(new Pet("三角形", 3));
        family2.getPets().add(new Pet("矩形", 4));

        switchPets(family1, family2);
        System.out.println(family1.toString());
        System.out.println(family2.toString());


    }

    public static void switchPets(Family f1, Family f2) {
        Pet pet1 = f1.getPets().first();
        Pet pet2 = f2.getPets().first();
        f1.getPets().add(pet2);
        f2.getPets().add(pet1);
        f1.getPets().remove(pet1);
        f2.getPets().remove(pet2);
    }

    private static void writeToFile(School s1, School s2, String path) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
        oos.writeObject(s1);
        oos.writeObject(s2);
        oos.close();
        System.out.println("写入完毕！");
    }

    private static void readFromFile(String path) throws IOException, ClassNotFoundException {
        //1 创建对象读取到二进制文件
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
        //2 不断地读取文件，直到文件末尾
        try {
            while (true) {
                Object obj = ois.readObject();
                //根据读取对象所在的类，做向下转型
                if (obj instanceof School) {
                    ((School) obj).toString();
                }
                System.out.println(obj);
            }
        } catch (EOFException e) {
            System.out.println("读取完毕！");
        }
        ois.close();
    }
}